/*
 * Copyright 2011 group coding kata nantes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.coding.kata.nantes.fizzbuzz.christophe;


import fr.coding.kata.nantes.fizzbuzz.commons.FizzBuzzInterface;

/**
 * Fizzer and Buzzer integer suite.
 */
public final class FizzerBuzzer implements FizzBuzzInterface {
// ------------------------------ FIELDS ------------------------------

    /**
     * the final size of fizz buzz message for 1 to 100.
     */
    private static final int SIZE_OF_FINAL_MESSAGE = 507;

    /**
     * the message cache.
     */
    private static final String MESSAGE_CACHE;

// -------------------------- STATIC METHODS --------------------------

    static {
        MESSAGE_CACHE = fizzerAndBuzzerOneToHundred();
    }

    /**
     * run FizzBuzz for numbers [1-100].
     *
     * @return the FizzBuzz message.
     */
    private static String fizzerAndBuzzerOneToHundred() {
        final StringBuilder message = new StringBuilder(SIZE_OF_FINAL_MESSAGE);
        for (int i = 1; i <= 100; i++) {
            fizzerAndBuzzer(i, message);
            message.append('\n');
        }

        return message.toString();
    }

    /**
     * fizzer and buzzer one number.
     *
     * @param anIntValue an number
     * @param anBuilder  an builder to complete
     */
    public static void fizzerAndBuzzer(final int anIntValue, final StringBuilder anBuilder) {
        boolean hasText = false;
        final EnumFizzBuzzWords[] values = EnumFizzBuzzWords.values();
        for (final EnumFizzBuzzWords value : values) {
            if (value.isMultiple(anIntValue)) {
                final String word = value.getWord();
                anBuilder.append(word);
                hasText = true;
            }
        }
        if (!hasText) {
            anBuilder.append(anIntValue);
        }
    }

// ------------------------ INTERFACE METHODS ------------------------


// --------------------- Interface Callable ---------------------

    @Override
    public String call() {
        return MESSAGE_CACHE;
    }
}
